package com.bao.wechat.files.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.bao.wechat.files.entity.XtjFiles;
import com.bao.wechat.files.service.FilesService;
import com.bao.wechat.files.service.IXtjFilesService;
import com.bao.wechat.utils.Constants;
import com.bao.wechat.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bao
 */
 
@RestController
@RequestMapping(value = "/xtjFiles", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class XtjFilesController extends ApiController {
    @Autowired
    private FilesService filesService;

    @Autowired
    private IXtjFilesService iXtjFilesService;

    @RequestMapping("/upload")
    public XtjFiles fileUpload(@RequestParam("file") MultipartFile file, Integer aid) {
        if(aid!=0){
            try {
                String url = filesService.fileUpload(Constants.multipartFileToFile(file));
                JSONObject jsonObject1 =JSONObject.parseObject(url);

                XtjFiles xtjFiles = new XtjFiles();

                xtjFiles.setAid(aid);
                xtjFiles.setName(file.getOriginalFilename());
                xtjFiles.setUrl("https://file.xintujing.cn/"+jsonObject1.get("bucketName")+"/"+jsonObject1.get("objectName"));
                iXtjFilesService.save(xtjFiles);
                return xtjFiles;

            } catch (Exception e) {
                System.out.println(e);
            }
        }
        return null;
    }

    @ResponseBody
    @RequestMapping(value = "/getFiles")
    public R getCoin(XtjFiles xtjFiles, Integer page, Integer limit){

        Page p = new Page<XtjFiles>(page,limit);
        QueryWrapper<XtjFiles> queryWrapper = new QueryWrapper<XtjFiles>();
        xtjFiles.setStatus(0);
        queryWrapper.setEntity(xtjFiles);
        IPage<XtjFiles> userIPage = iXtjFilesService.page(p,queryWrapper);
        return  R.ok().put("data",userIPage.getRecords()).put("count", iXtjFilesService.count(queryWrapper));
    }
    
    @ResponseBody
    @RequestMapping(value = "/getFileList")
    public R getFileList(XtjFiles xtjFiles, Integer page, Integer limit){

        Page p = new Page<XtjFiles>(page,limit);
        QueryWrapper<XtjFiles> queryWrapper = new QueryWrapper<XtjFiles>();
        queryWrapper.setEntity(xtjFiles);
        IPage<XtjFiles> userIPage = iXtjFilesService.page(p,queryWrapper);
        return  R.ok().put("data",userIPage.getRecords()).put("count", iXtjFilesService.count(queryWrapper));
    }
    
    /**
	 * 资源上线下线
	 * @Description
	 *  @author bao
	 *  @data Jul 1, 2020 9:03:59 AM
	 */
	@RequestMapping(value = "/FilesDelete")
	public R FilesDelete(String id,Integer status){
		XtjFiles xtjActivity = iXtjFilesService.getById(id);
		xtjActivity.setStatus(status);
		iXtjFilesService.updateById(xtjActivity);
		return  R.ok();
	}


}
